package com.xiaodou.mjf.application.core.service.impl;

import java.util.List;

import javax.annotation.Resource;
import javax.persistence.criteria.Predicate;
import javax.transaction.Transactional;

import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Service;

import com.xiaodou.mjf.application.core.repository.LoanRepository;
import com.xiaodou.mjf.application.core.service.LoanService;
import com.xiaodou.mjf.entity.core.Loan;
import com.xiaodou.mjf.infrastructure.common.app.JpaSimpleService;

/***
 *
 * Loan业务访问实现
 * 
 * @author XiaoDou.com Code Generator
 * @version 1.0 2014-10-29 17:37:52
 *
 */
@Service
@Transactional
public class LoanServiceImpl extends JpaSimpleService<Loan, String> implements
		LoanService {

	Logger logger = LoggerFactory.getLogger(LoanServiceImpl.class);

	@Resource
	LoanRepository loanRepository;

	@Override
	protected JpaRepository<Loan, String> getRepository() {
		return loanRepository;
	}

	@Override
	public Page<Loan> pageAll(Loan entity) {
		return loanRepository.findAll(
				(root, query, cb) -> {

					Predicate p1 = cb.equal(root.get("status").as(Integer.class), 0);
					Predicate p2 = cb.equal(root.get("term").as(Integer.class),entity.getTerm());
					return query.where(p1, p2).getRestriction();
				},
				preparePage(entity, new Sort(new Sort.Order(Sort.Direction.ASC,"memberId"), new Sort.Order(Sort.Direction.DESC,"applyDate"))));
	}


	
	@Override
	public Page<Loan> page(Loan entity) {
		return loanRepository.findAll(
				(root, query, cb) -> {
					
					Predicate p1 = cb.equal(root.get("memberId").as(String.class),entity.getMemberId());
					return query.where(p1).getRestriction();
				},
				preparePage(entity,new Sort(new Sort.Order(Sort.Direction.DESC,"createTime"))));
//				preparePage(entity, new Sort(new Sort.Order(Sort.Direction.ASC,"memberId"), new Sort.Order(Sort.Direction.DESC,"applyDate"))));
	}

	
	@Override
	public Page<Loan> pageAllData(Loan entity) {
		return loanRepository.findAll((root,query,cb) -> {
	           Predicate p1 = cb.equal(root.get("status").as(Integer.class),entity.getStatus());   	
	           Predicate p2 = cb.like(root.get("memberId").as(String.class), "%" + entity.getMemberId() + "%");
	           
	           if(entity.getStatus().equals(-1)){
	         	  return query.where(p2).getRestriction();     
	             }
	           
	           return query.where(p1,p2).getRestriction();
	        },preparePage(entity,new Sort(Direction.DESC,"applyDate")));
//		return loanRepository.findAll(preparePage(entity,new Sort(Direction.DESC,"applyDate")));
	}
	
//	@Override
//	public Page<Loan> pageByDate(Loan entity) {
//		return loanRepository.findAll((root,query,cb) -> {
//	           Predicate p1 = cb.equal(root.get("status").as(Integer.class),entity.getStatus());	
//	           Predicate p2 = cb.greaterThanOrEqualTo(root.get("createTime").as(Date.class), DateUtils.getStartMonth(entity.getCreateTime()));	           
//	           Predicate p4 = cb.lessThanOrEqualTo(root.get("createTime").as(Date.class), DateUtils.getEndMonth(entity.getCreateTime()));
//	           Predicate p3 = cb.equal(root.get("memberId").as(String.class),entity.getMemberId()); 
//	           return query.where(p1,p2,p3,p4).getRestriction();
//	        },preparePage(entity,new Sort(Direction.DESC,"applyDate")));
////		return loanRepository.findAll(preparePage(entity,new Sort(Direction.DESC,"applyDate")));
//	}

    @Override
    public List<Loan> findByFinanceStatusAndStatus(int financeStatus, int status) {
        return loanRepository.findByFinanceStatusAndStatus(financeStatus,status);
    }

	@Override
	public List<Loan> findByDate(String year,String month,String id) {
		 //字段：stage_end_time between 本月第一天0点 and 本月最后一天24点
        DateTime dateStart = DateTimeFormat.forPattern("yyyy-M-d HH:mm:ss").parseDateTime(Integer.parseInt(year) + "-" + Integer.parseInt(month) + "-1 00:00:00");
        DateTime dateEnd = dateStart.plusMonths(1).minusMillis(1);                
		return loanRepository.findByDate(dateStart.toDate(),dateEnd.toDate(),id);

	}
}
